﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;

namespace WPFSample.PopupContextMenuSample
{
    class PopupContextMenu : Window
    {
        ContextMenu menu;
        MenuItem itemBold, itemItalic;
        MenuItem[] itemDecor;
        Inline inlClicked;

        public PopupContextMenu()
        {
            this.Title = "Popup context Menu";

            this.menu = new ContextMenu();

            this.itemBold = new MenuItem();
            this.itemBold.Header = "Bold";
            this.menu.Items.Add(this.itemBold);

            this.itemItalic = new MenuItem();
            this.itemItalic.Header = "Italic";
            this.menu.Items.Add(this.itemItalic);

            TextDecorationLocation[] locs = (TextDecorationLocation[])Enum.GetValues(typeof(TextDecorationLocation));

            this.itemDecor = new MenuItem[locs.Length];

            for (int i = 0; i < locs.Length; i++)
            {
                TextDecoration decor = new TextDecoration();
                decor.Location = locs[i];

                itemDecor[i] = new MenuItem();
                itemDecor[i].Header = locs[i].ToString();
                itemDecor[i].Tag = decor;
                menu.Items.Add(itemDecor[i]);
            }

            menu.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(this.MenuOnClick));

            TextBlock text = new TextBlock();
            text.FontSize = 32;
            text.HorizontalAlignment = HorizontalAlignment.Center;
            text.VerticalAlignment = VerticalAlignment.Center;
            this.Content = text;

            string strQuote = "To be, or not to be, that is the question";
            string[] words = strQuote.Split();

            foreach (string str in words)
            {
                Run run = new Run(str);
                run.TextDecorations = new TextDecorationCollection();
                text.Inlines.Add(run);
                text.Inlines.Add(" ");
            }
        }

        private void MenuOnClick(object sender, RoutedEventArgs e)
        {
            MenuItem item = e.Source as MenuItem;
            item.IsChecked ^= true;

            if (item == this.itemBold)
            {
                this.inlClicked.FontWeight = (item.IsChecked ? FontWeights.Bold : FontWeights.Normal);
            }
            else if (item == this.itemItalic)
            {
                this.inlClicked.FontStyle = (item.IsChecked ? FontStyles.Italic : FontStyles.Normal);
            }
            else
            {
                if (item.IsChecked)
                {
                    this.inlClicked.TextDecorations.Add(item.Tag as TextDecoration);
                }
                else
                {
                    this.inlClicked.TextDecorations.Remove(item.Tag as TextDecoration);
                }
            }
        }

        protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseRightButtonUp(e);
            if ((this.inlClicked = e.Source as Inline) != null)
            {
                this.itemBold.IsChecked = (this.inlClicked.FontWeight == FontWeights.Bold);
                this.itemItalic.IsChecked = (this.inlClicked.FontStyle == FontStyles.Italic);
                foreach (MenuItem item in this.itemDecor)
                {
                    item.IsChecked = this.inlClicked.TextDecorations.Contains(item.Tag as TextDecoration);
                }

                menu.IsOpen = true;
                e.Handled = true;
            }
        }
    }
}
